﻿using CTP;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 每日合约信息落地
{
    internal class Program
    {
        static CTPTraderAdapter trade;
        static string TradeAddress;
        static string TradePort;
        static string BrokerID;
        static string ClientID;
        static string AuthCode;
        static string ClientInfo;
        static string FundAccount;
        static string Password;
        static string BDAddress;
        static string BDPort;
        static string BDName;
        static string BDUserID;
        static string BDPassword;
        static int reqID = 0;
        static void Main(string[] args)
        {
            // 读取配置项
            TradeAddress = ConfigurationManager.AppSettings["TradeAddress"];
            TradePort = ConfigurationManager.AppSettings["TradePort"];
            BrokerID = ConfigurationManager.AppSettings["BrokerID"];
            ClientID = ConfigurationManager.AppSettings["ClientID"];
            AuthCode = ConfigurationManager.AppSettings["AuthCode"];
            ClientInfo = ConfigurationManager.AppSettings["ClientInfo"];
            FundAccount = ConfigurationManager.AppSettings["FundAccount"];
            Password = ConfigurationManager.AppSettings["Password"];
            BDAddress = ConfigurationManager.AppSettings["BDAddress"];
            BDPort = ConfigurationManager.AppSettings["BDPort"];
            BDName = ConfigurationManager.AppSettings["BDName"];
            BDUserID = ConfigurationManager.AppSettings["BDUserID"];
            BDPassword = ConfigurationManager.AppSettings["BDPassword"];
            trade=new CTPTraderAdapter();
            trade.OnFrontConnected += Trade_OnFrontConnected;
            trade.OnFrontDisconnected += Trade_OnFrontDisconnected;
            trade.OnRspAuthenticate += Trade_OnRspAuthenticate;
            trade.RegisterFront($"tcp://{TradeAddress}:{TradePort}");
            trade.Init();
            Console.ReadLine();
        }

        private static void Trade_OnRspAuthenticate(ThostFtdcRspAuthenticateField pRspAuthenticateField, ThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {
            throw new NotImplementedException();
        }

        private static void Trade_OnFrontDisconnected(int nReason)
        {
            throw new NotImplementedException();
        }

        private static void Trade_OnFrontConnected()
        {
            Log("前置已连接");
            trade.ReqAuthenticate(new ThostFtdcReqAuthenticateField() { AppID=ClientID, AuthCode=AuthCode, BrokerID=BrokerID,UserID=FundAccount, UserProductInfo = ClientInfo }, reqID++);
        }
        private static void Log(string message) 
        {
            Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}>>{message}");
        }
    }
}
